/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.zzdt4j.core.enable;

import cn.hutool.core.util.StrUtil;
import cn.zzdt4j.core.config.ConfigEmptyException;
import cn.zzdt4j.threadpool.dynamic.api.BootstrapPropertiesInterface;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * Before check configuration
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023/10/12 1:12
 */
@Configuration
@AllArgsConstructor
public class BeforeCheckConfiguration {

    private final String bootstrapPropertiesClassName = "cn.zzdt4j.springboot.starter.config.BootstrapProperties";

    private final String bootstrapConfigPropertiesClassName = "cn.zzdt4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties";

    @Bean
    public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Autowired(required = false) BootstrapPropertiesInterface properties,
                                                                                 ConfigurableEnvironment environment) throws ConfigEmptyException {
        // 提前检查，是否开启动态线程配置
        boolean checkFlag = properties != null && properties.getEnable();

        if (checkFlag) {
            String propertiesClassName = properties.getClass().getName();
            switch (propertiesClassName) {
                case bootstrapPropertiesClassName: {
                    String namespace = properties.getNamespace();
                    if (StrUtil.isBlank(namespace)) {
                        throw new ConfigEmptyException("Web server failed to start. The dynamic thread pool namespace is empty",
                                "Please check whether the [spring.dynamic.thread-pool.namespace] configuration is empty or an empty string.");
                    }
                    break;
                }
                case bootstrapConfigPropertiesClassName: {

                }
                default:
            }

        }
        return new BeforeCheck();
    }

    public class BeforeCheck {
    }
}
